package org.zjt.review

/**
  * DESC    
  *
  * @author
  * @create 2017-06-20 下午3:15
  **/


class Father(val name: String) {
  var age = 0
}


class Class1(override val name: String, addr: String = "北京") extends Father(name) {
  def printInfo(): Unit = {
    println(s"$this")
  }

  override def toString: String = "name:%s\tage:%s\taddr:%s".format(name, age, addr)
}


class Class2(val name2: String, addr: String = "北京") extends Father(name2) {
  def printInfo(): Unit = {
    println(s"$this")
  }

  override def toString: String = "name:%s\tage:%s\taddr:%s".format(name, age, addr)
}

// 私有构造方法
class Marker private(val color:String) {
  println("创建" + this)        //构造函数执行语句
  override def toString(): String = "颜色标记："+ color

}


/**
  *  伴生对象，与类共享名字，可以访问类的私有属性和方法
  *  伴生对象是静态变量。
  */
object Marker{


  // map对象
  private val markers: Map[String, Marker] = Map(
    "red" -> new Marker("red"),
    "blue" -> new Marker("blue"),
    "green" -> new Marker("green")
  )


  def apply(color:String) = {
    if(markers.contains(color)) markers(color) else null
  }


  def getMarker(color:String) = {
    if(markers.contains(color)) markers(color) else null
  }


  def main(args: Array[String]) {
    println(Marker("red"))
    // 单例函数调用，省略了.(点)符号
    println(Marker getMarker "blue")

    println(Marker toString)
  }

}


object classStart {

  def main(args: Array[String]): Unit = {
    val info = new Class1("zhang")
    info.age = 12
    info.printInfo()
  }
}